package com.css.dzj.dzjap.modules.jgxwother.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.apunit.entity.ApUnitEntity;
import com.css.dzj.dzjap.modules.jgxwother.entity.JgJgxwOtherActionEntity;
import com.css.dzj.dzjap.modules.jgxwother.service.OtherService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/other")
public class OtherController {

    @Autowired
    private OtherService otherService;
    @Autowired
    BaseService baseService;

    //查询其他行为信息表
    @GetMapping(value = "/getOther")
    public R getOther(@RequestParam("actionId") String actionId) {
        baseService.logSuccess(3, "查询成功", "2");
        return R.ok().put("data", otherService.getOther(actionId));
    }


    // 添加其他行为信息表
    @PostMapping(value = "/addOther")
    public R addOther(@RequestBody JgJgxwOtherActionEntity other) {
        try {
            String flag = otherService.addOther(other);
            if (!"0".equals(flag)) {
                R r = new R();
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
                baseService.logSuccess(3, "添加成功", "2");
                return r;
            } else {
                baseService.logFailed(3, "添加失败", "2");
                return R.error("添加失败");
            }
        } catch (Exception e) {
            baseService.logFailed(3, "添加失败", "2");
            return R.error(e.getMessage());
        }
    }

    // 修改其他行为信息表
    @PostMapping(value = "/updateOther")
    public R updateOther(@RequestBody JgJgxwOtherActionEntity other) {

        try {
            JgJgxwOtherActionEntity aOther = otherService.getOther(other.getActionId());
            BeanUtil.copyBeanNotNullToBean(other, aOther);
            aOther.setUpdateTime(new Date());
            otherService.update(aOther);
            baseService.logSuccess(3, "修改成功", "2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3, "修改失败", "2");
            return R.error(e.getMessage());
        }
    }


    // 删除其他行为信息表
    @PostMapping(value = "/deleteOther")
    public R deleteOther(@RequestParam("actionId") String actionId) {
        if (otherService.deleteOther(actionId)) {
            baseService.logSuccess(3, "删除成功", "2");
            return R.ok("操作成功!");
        } else {
            baseService.logFailed(3, "删除失败", "2");
            return R.error("操作失败!");
        }
    }

    /**
     * 分页查询所有其他行为信息表
     */
    @GetMapping(value = "/getAllOther")
    public R getAllOther(@RequestParam(required = false) Integer pageNo,
                         @RequestParam(required = false) Integer pageSize
    ) {

        PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
        baseService.logSuccess(3, "查询成功", "2");
        return R.ok().put("data", otherService.getAllOther(pageRequest));
    }

    /**
     * 多条件查询所有其他行为信息表
     */
    @GetMapping(value = "/getAllConOther")
    public R getAllConOther(@RequestParam String superviseItemCode,
                            @RequestParam(required = false) String recordUniqueIdentity,
                            @RequestParam(required = false) String actionName,
                            @RequestParam(required = false) String actionCode,
                            @RequestParam(required = false) String checkBehaviorNumber,
                            @RequestParam(required = false) String superviseObject,
                            @RequestParam(required = false) Integer pageNo,
                            @RequestParam(required = false) Integer pageSize) {

        PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
        baseService.logSuccess(3, "查询成功", "2");
        return R.ok().put("data", otherService.getAllConOther(pageRequest, superviseItemCode, recordUniqueIdentity, actionName, actionCode, checkBehaviorNumber, superviseObject));
    }

    /***
     * 获取其他行为
     * @return
     */
    @GetMapping("/other")
    public R countOther() {
        HashMap count = otherService.getCount();
        return R.ok().put("count", count);
    }

    /***
     * 获取其他行为
     * @return
     */
    @GetMapping("/otheryear")
    public R countOtherByYear(String year) {
        HashMap count = otherService.getCountByYear(year);
        return R.ok().put("count", count);
    }

    /***
     * 获取当前登陆人的机构的其他行为
     * @param request
     * @return
     */
    @GetMapping("/getother")
    public R getOpOther(HttpServletRequest request) {
        Long count = otherService.getOpCheck(request);
        return R.ok().put("count", count);
    }


    /******************************** 门户接口-开始 ********************************/

    @GetMapping(value = "/getPage")
    public QueryResults getPage(@RequestParam(required = false) String superviseItemCode,
                                @RequestParam(required = false) String recordUniqueIdentity,
                                @RequestParam(required = false) String actionName,
                                @RequestParam(required = false) String actionCode,
                                @RequestParam(required = false) String checkBehaviorNumber,
                                @RequestParam(required = false) String superviseObject,
                                @RequestParam(required = false) Integer pageNo,
                                @RequestParam(required = false) Integer pageSize) {

        PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
        QueryResults<JgJgxwOtherActionEntity> page = otherService.getAllConOther(pageRequest,
                superviseItemCode, recordUniqueIdentity, actionName, actionCode, checkBehaviorNumber, superviseObject);
        return page;
    }

    /**
     * 获取其他行为List
     *
     * @return
     */
    @GetMapping(value = "/getList")
    public List getList(@RequestParam(required = false) String superviseItemCode,
                        @RequestParam(required = false) String recordUniqueIdentity,
                        @RequestParam(required = false) String actionName,
                        @RequestParam(required = false) String actionCode,
                        @RequestParam(required = false) String checkBehaviorNumber,
                        @RequestParam(required = false) String superviseObject) {
        List<JgJgxwOtherActionEntity> list = otherService.getList(
                superviseItemCode, recordUniqueIdentity, actionName, actionCode, checkBehaviorNumber, superviseObject);
        return list;
    }

    /**
     * 根据ids(逗号分隔)查询List数据
     *
     * @param ids
     * @return
     */
    @GetMapping("/getListByIds")
    public List exportToExcel(@RequestParam String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        List<JgJgxwOtherActionEntity> list = otherService.queryByIds(idList);
        return list;
    }

    /**
     * 获取总数
     *
     * @return
     */
    @GetMapping("/getTotalCount")
    public Integer getTotalCount() {
        Integer count = otherService.getTotalCount();
        return count;
    }
    /********************************* 门户接口-结束 *******************************/
}
